﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography.Pkcs;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traceability.Commont.DTOs.SectionDto;

namespace Butcher.winform.EnterpriseForms
{
    public partial class Departmentview : Form
    {
        public Departmentview()
        {
            InitializeComponent();
            this.dataGridView1.ColumnCount = 4;
            this.dataGridView1.Width = 800;
            dataGridView1.Columns[0].Name = "主键";
            dataGridView1.Columns[1].Name = "部门上级";
            dataGridView1.Columns[2].Name = "部门名称";
            dataGridView1.Columns[3].Name = "部门类型";
            GETHtml();

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }



        public void GETHtml()
        {


            string url = @"http://localhost:5229/api/Section/SectionHtml";
            HttpClient client = new HttpClient();
            QueryCondition condition = new QueryCondition();
            condition.pageindex = 1;
            condition.pagesize = 2;
            condition.sectionName = "";
            condition.sectionType = 0;
            condition.sectionStatus = 0;
            var content = new StringContent(JsonSerializer.Serialize<QueryCondition>(condition), Encoding.UTF8);
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject josnresult = JObject.Parse(result);
            var results = josnresult["neWsections"].Value<JArray>();
            List<NEWsection> ssss = results.ToObject<List<NEWsection>>();

            var c = 0;


            foreach (var item in ssss)
            {

                addrow(new NEWsection());
                dataGridView1.Rows[c].Cells[0].Value = item.Departmentid;
                dataGridView1.Rows[c].Cells[1].Value = item.Levelid;
                dataGridView1.Rows[c].Cells[2].Value = item.DepartmentName;
                if (item.Typeid == 1)
                {
                    dataGridView1.Rows[c].Cells[3].Value = "生产部";
                }
                else
                {
                    dataGridView1.Rows[c].Cells[3].Value = "销售部";
                }


                c++;

            }
        }


        public class QueryCondition
        {


            public int pagesize { get; set; }
            public int pageindex { get; set; }
            public string sectionName { get; set; }

            public int sectionType { get; set; }

            public int sectionStatus { get; set; }

        }


        private void addrow(NEWsection stu)
        {
            object[] row =
            {
                stu.Departmentid,
                stu.DepartmentName,
                stu.Levelid,
                stu.Typeid
            };
            dataGridView1.Rows.Add(row);

        }
    }
}
